//
//  originalStatusView.swift
//  sinazd
//
//  Created by apple on 16/3/12.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit
let StatusCellMargin: CGFloat = 10

class originalStatusView: UIView {
    
    //添加一个属性保存originalStatusView的底部的约束
    var bottomConstraint:Constraint?
    
    
    //调用set方法的时候进行赋值
    var originalStatus: Status? {
        didSet {
            //绑定模型数据
            iconView.sd_setImageWithURL(originalStatus?.user?.headImageURL)
            nameLabel.text = originalStatus?.user?.name
            mbrank_image.image = originalStatus?.user?.mbrank_image
            verified_image.image = originalStatus?.user?.verified_type_image
            // TODO: 后期完善  OC中可以使用警告来标示待做事项
            // sourceLabel.text = status?.source
            // timeLabel.text = status?.created_at
            contentLabel.text = originalStatus?.text
            
            bottomConstraint?.uninstall()
            
            if let urls = originalStatus?.imgUrls where urls.count != 0 {
                
                pictureView.imgUrls = urls
                
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
            }else{
                
                pictureView.imgUrls = nil
                
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                
            }
            
            
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //给自己设置添加控件
    func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        //添加子视图
        
        addSubview(sepView)
        sepView.backgroundColor = UIColor.purpleColor()
        
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(mbrank_image)
        addSubview(verified_image)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        //添加配图
        addSubview(pictureView)
        
        
        //配图约束
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.top.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLabel.snp_left)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            
            make.size.equalTo(CGSize(width: 100, height: 100))
            
        }
        
        
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 36, height: 36))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        mbrank_image.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            
        }
        
        verified_image.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
            
        }
        
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel.snp_top)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
            
        }
        
        self.snp_makeConstraints { (make) -> Void in
            //make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
            
        }
        
        iconView.layer.cornerRadius = 18
        iconView.clipsToBounds = true
        
        
    }
    
    
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var mbrank_image: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var verified_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var nameLabel: UILabel = UIFactory.labelWith("25期的童鞋们", textColor: UIColor.darkGrayColor(), fontSize: 15)
    private lazy var timeLabel: UILabel = UIFactory.labelWith("25:25", textColor: UIColor.darkGrayColor(), fontSize: 11)
    private lazy var sourceLabel: UILabel = UIFactory.labelWith("隔壁老王", textColor: UIColor.darkGrayColor(), fontSize: 11)
    private lazy var contentLabel: UILabel = UIFactory.labelWith("你若安好,便是晴天", textColor: UIColor.darkGrayColor(), fontSize: 14,alignment:.Left)
    
    
    private lazy var pictureView: statusPictureView = statusPictureView()
    
    
    //自己添加分割线
    private lazy var sepView:UIView = UIView()
    
}





